﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using OnlineMealOrdering.Domain;
using OnlineMealOrdering.Extends.MvcPager;
using OnlineMealOrdering.Common;
using System.ComponentModel;
using Framework.Mvc.Attributes;
using System.Web.Mvc;


namespace OnlineMealOrdering.Areas.WebSiteManage.Models
{
    public class AccountModel : ModelBase
    {
        [DisplayName("编号")]
        public int? Id { get; set; }

        [DisplayName("登陆账号")]
        public string UserCode { get; set; }

        [DisplayName("名字")]
        public string Name { get; set; }

        [DisplayName("密码")]
        [DataTablesVisible(false)]
        public string Password { get; set; }

        [DisplayName("区域")]
        public string Area { get; set; }

        [DisplayName("地址")]
        public string Address { get; set; }

        [DisplayName("账号类型")]
        public string AccountType { get; set; }

        [DisplayName("用户状态")]
        public string UserStatus { get; set; }

        [DataTablesVisible(false)]
        public decimal Longitude { get; set; }

        [DataTablesVisible(false)]
        public decimal Latitude { get; set; }

        [DataTablesVisible(false)]
        public List<string> Roles { get; set; }

        [DataTablesVisible(false)]
        [DisplayName("头像")]
        public string FaceImage { get; set; }

        private string _StrRoles;

        [DisplayName("角色")]
        public string StrRoles
        {
            get
            {
                if (Roles != null)
                {
                    Roles.ForEach(a =>
                    {
                        _StrRoles += RolesDefinition.GetAllRolesDefinition()[a] + ",";
                    });
                    _StrRoles = string.IsNullOrEmpty(_StrRoles) ? "" : _StrRoles.TrimEnd(',');

                }
                return _StrRoles;
            }
            set
            {
                _StrRoles = value;
            }
        }

        [DisplayName("创建日期")]
        public DateTime CreatedDate { get; set; }

        [DataTablesVisible(false)]
        public List<SelectListItem> AllUserRoles
        {
            get
            {
                var roles = new List<SelectListItem>();
                if (roles.Count == 0)
                {
                    RolesDefinition.GetAllRolesDefinition().AsQueryable().ToList().ForEach(a =>
                    {
                        roles.Add(new SelectListItem() { Selected = Roles.Contains(a.Key), Value = a.Key, Text = a.Value });
                    });
                }
                return roles;
            }
        }

        [DataTablesVisible(false)]
        public List<SelectListItem> AllUserStatus
        {
            get
            {

                var allStatus = from a in EnumHelper.GetEmumAllValue(typeof(OnlineMealOrdering.Domain.UserStatus))
                                select new SelectListItem
                                {
                                    Text = a.Value,
                                    Value = a.Key.ToString(),
                                    Selected = (a.Value == UserStatus)
                                };
                return allStatus.ToList();
            }
        }

        [DataTablesVisible(false)]
        public string TempUserStatus { get; set; }

        [DataTablesVisible(false)]
        public string TempStrRoles { get; set; }

        public static PagedList<AccountModel> GetAllUser(int? pageindex, int pagesize)
        {
            PagedList<AccountModel> list =
                Ctx.EntitySet<UserEntity>().OrderBy(a => a.Id).ToPagedList(a => a.ToAccountModel(), pageindex ?? 1, pagesize);
            return list;
        }

        public static AccountModel Create(AccountModel model)
        {
            var entity = UserEntity.CreateUser(model.UserCode, model.Name, model.Password, "", "", model.Area, model.Address, new List<string>() { RolesDefinition.ConsumerRoleCode });
            Ctx.Add<UserEntity>(entity);
            Ctx.SubmitChanges();
            return entity.ToAccountModel();
        }

        public static AccountModel GetUser(string usercode)
        {
            var entity = Ctx.EntitySet<UserEntity>(a => a.UserCode == usercode).FirstOrDefault();
            return entity.ToAccountModel();
        }
    }
}